package com.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.entity.Book;
import com.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-03 11:29:37
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;


    @RequestMapping("all")
    @ResponseBody
    public Page<Book> all(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "9") Integer pageSize
    ) {
//        List<Book> bookList = bookService.list();

        Page<Book> page = new Page<>(currentPage, pageSize);

        Page<Book> bookPage = bookService.page(page);

        return bookPage;
    }

    @RequestMapping("single")
    public String single(Long bookId, Model model) {
        Book book = bookService.getById(bookId);
        model.addAttribute("book", book);

        return "singleBook";
    }

    @RequestMapping("add")
    public void add(Book book) {
        System.out.println(book);

        book.setImgsrc("http://localhost:10002/imgs/11.jpg");
        book.setCollectioncount(0);
        book.setBuycount(0);
        book.setReadcount(0);
        book.setCreatetime(new Date());
        book.setUpdatetime(new Date());
        book.setState(2);
        book.setVersion(0);

        bookService.save(book);
    }

    @RequestMapping("delete")
    public void delete(Integer bookId) {
        bookService.removeById(bookId);

    }



    @RequestMapping("update")
    public void update(Book book) {


        bookService.updateById(book);
    }
}

